<template>
  <div class="dashboard-container">
    <div class="navbarchild">
      <div @click="$router.go(-1)">
        <i class="el-icon-back"></i>
        <span>返回</span>
      </div>
      <el-card style="height: 120px; margin-bottom: 20px; margin-top: 10px">
        <!-- 给el-form绑定form -->
        <el-form label-width="80px" :model="form" :inline="true">
          <!-- <el-form-item>
          <el-select v-model="form.servetypeid" placeholder="服务分类">
            <el-option
              v-for="a in reqCategory1"
              :key="a.id"
              :label="a.name"
              :value="a.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchclick"
          style="margin-left: 12px"
          >筛选</el-button
        >
        <el-button @click="resetSearch">重置</el-button> -->
          <el-button @click="addrenyuan" type="success">添加服务</el-button>
        </el-form>
      </el-card>
      <el-table :data="tableData" border>
        <!-- style="width: 100%; font-size: 10px" -->
        <el-table-column prop="id" label="ID" width="100"> </el-table-column>
        <el-table-column prop="name" label="名称" align="center">
        </el-table-column>
        <el-table-column label="封面图" align="center" width="180">
          <template slot-scope="scope">
            <el-image :src="scope.row.image" fit="cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="轮播图" align="center" width="180">
          <template slot-scope="scope" v-if="scope.row.images">
            <el-carousel height="160px">
              <el-carousel-item
                style="
                  display: flex;
                  justify-content: center;
                  align-items: center;
                "
                v-for="(item, index) in scope.row.images.split(',')"
                :key="index"
              >
                <el-image :src="item" fit="cover"></el-image>
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-table-column>

        <el-table-column prop="recommend" label="是否推荐" align="center">
          <template slot-scope="scope" v-if="scope.row.recommend">
            <span>{{ scope.row.recommend | statusfilter }}</span>
            <el-button
              style="margin-left: 12px"
              type="success"
              @click="isTuijian(scope.row)"
              size="small"
              >是否推荐</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="上下架" align="center">
          <template slot-scope="scope" v-if="scope.row.images">
            <!---->
            <el-switch
              v-model="scope.row.switch"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="change($event, scope.row.id)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="450">
          <template slot-scope="scope">
            <el-button
              type="success"
              @click="previewpreview(scope.row)"
              size="small"
              >夜班加班费详情</el-button
            >
            <el-button type="info" @click="shezhi(scope.row)" size="small"
              >设置夜班加班费</el-button
            >
            <!-- <el-button
              type="success"
              @click="addadd(scope.row)"
              size="small"
              v-if="!scope.row.images"
              >添加服务</el-button
            > -->
            <!-- v-if="scope.row.images" -->
            <el-button
              type="warning"
              @click="tiaozhuanguige(scope.row)"
              size="small"
              >不同区县规格</el-button
            >
            <!-- 编辑服务分类的 -->
            <!-- <el-button
            type="primary"
            @click="changerenyuan(scope.row)"
            v-if="!scope.row.images"
            size="small"
            >编辑</el-button
          > -->
            <!-- 编辑服务的 -->
            <el-button
              type="primary"
              @click="changerenyuan11(scope.row)"
              v-if="scope.row.images"
              size="small"
              >编辑</el-button>
            <el-button
              type="danger"
              @click="deldel(scope.row)"
              size="small"
              v-if="!scope.row.images"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :currentPage="this.form.pageNo"
        :size="this.form.pageSize"
        :total="total"
        @changelimit="changelimit"
        @changecurrentPage="changecurrentPage"
      ></Pagination>
    </div>

    <add ref="add" @refresh="refresh"></add>
    <preview ref="preview"></preview>
    <istuijian ref="istuijian" @refresh="refresh"></istuijian>
    <serveradd ref="serveradd" @refresh="refresh"></serveradd>
    <servenightupdate
      ref="servenightupdate"
      @refresh="refresh"
    ></servenightupdate>
  </div>
</template>

<script>
import {
  getservelist,
  shangxiaservesxj,
  delservetypedel,
  getservetypes,
} from "@/api/server";
import add from "./modules/add";
import preview from "./modules/preview";
import istuijian from "./modules/istuijian";
import serveradd from "./modules/serveradd";
import servenightupdate from "./modules/servenightupdate";
export default {
  name: "Dashboard",
  components: {
    add,
    istuijian,
    serveradd,
    preview,
    servenightupdate,
  },
  data() {
    return {
      form: {
        servetypeid: "",
        pageNo: 1,
        pageSize: 5,
      },
      total: 0,
      tableData: [],
      reqCategory1: [],
    };
  },
  methods: {
    tiaozhuanguige(val) {
      this.$router.push({
        name: "serveguigelist",
        query: {
          serveid: val.id,
        },
      });
    },
    async change(e, id) {
      let data = {
        id: Number(id),
        status: e ? 1 : 2,
      };
      console.log(data);
      let result = await shangxiaservesxj(data);
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success",
        });
      }
      console.log(result);
      this.getservetypes1();
    },

    searchclick() {
      this.getservetypes1();
    },
    resetSearch() {
      this.form.servetypeid = "";
      this.getservetypes1();
    },
    addrenyuan() {
      this.$refs.serveradd.add();
    },
    // 子传父:自定义事件
    refresh() {
      this.getservetypes1();
    },
    // 获取服务
    async getservetypes1() {
      let result = await getservelist(this.form);
      console.log(result);
      if (result.code == 0) {
        this.tableData = result.data.data;
        this.tableData.forEach((item) => {
          if (item.status == 1) {
            this.$set(item, "switch", true);
          } else {
            this.$set(item, "switch", false);
          }
        });
      }
    },
    // 获取服务分类
    async getservetypes2() {
      let result = await getservetypes();
      this.reqCategory1 = result.data;
    },
    changelimit(val) {
      this.form.pageSize = val;
      this.getservetypes1();
    },
    changecurrentPage(val) {
      this.form.pageNo = val;
      this.getservetypes1();
    },
    addadd(val) {
      console.log(val);
      this.$refs.serveradd.add(val);
    },
    shezhi(val) {
      this.$refs.servenightupdate.add(val);
    },
    previewpreview(val) {
      this.$refs.preview.preview(val);
    },
    changerenyuan(val) {
      this.$refs.add.edit(val);
    },
    changerenyuan11(val) {
      this.$refs.serveradd.edit(val);
    },
    isTuijian(val) {
      this.$refs.istuijian.edit(val);
    },
    async deldel(val) {
      let result = await delservetypedel({ id: val.id });
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "删除成功",
          type: "error",
        });
        this.getservetypes1();
      }
    },
  },
  watch: {
    "form.servetypeid": {
      handler(newVal) {
        if (newVal == "") {
          this.getservetypes1();
        }
      },
    },
  },
  filters: {
    statusfilter(val) {
      if (val == 1) {
        return "正常";
      } else {
        return "推荐";
      }
    },
    serverfilter(val) {
      if (that.reqcategory2 && that.reqcategory2.length > 0) {
        return that.reqcategory2.filter((item) => item.id == val)[0].name;
      } else {
        return "loading";
      }
    },
    datefilter(val) {
      var date = new Date(parseInt(val) * 1000);
      var Year = date.getFullYear();
      var Moth =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var Minute =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var Sechond =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      var GMT =
        Year +
        "-" +
        Moth +
        "-" +
        Day +
        "   " +
        Hour +
        ":" +
        Minute +
        ":" +
        Sechond;

      return GMT;
    },
  },
  created() {
    this.$nextTick(() => {
      this.form.servetypeid = Number(this.$route.query.servetypeid);
      // 获取服务
      this.getservetypes1();
      // 获取服务分类
      this.getservetypes2();
    });
  },
  // mounted() {

  // },
};
</script>

<style lang="scss" scoped>
// .dashboard {
//   &-container {
//     margin: 30px;
//   }
//   &-text {
//     font-size: 30px;
//     line-height: 46px;
//   }
// }
.navbarchild {
  background: white;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  height: 50px;
  // width: 90%;
  line-height: 50px;
  padding: 0px 20px;
  cursor: pointer;
}
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 150px;
  height: 150px;
}

::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

::v-deep .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

::v-deep .avatar {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;

  max-width: 150px;
  max-height: 150px;
  margin: 0 auto;
  display: block;
}
</style>
